# -*- coding: utf-8 -*-

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('dige.png')
#腐蚀操作
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations=1)#iterations表示操作次数
#膨胀操作
dilate = cv2.dilate(erosion,kernel,iterations=1)  #腐蚀操作过后对图片造成一定损伤，用膨胀操作还原图像
res = np.hstack((img, erosion, dilate))
cv2.imshow('erosion', res)
cv2.imwrite('contrast2.png', res)
#contrast2为原图、腐蚀、膨胀操作对比图
cv2.waitKey(0)
cv2.destroyAllWindows()
##################################################
# img = cv2.imread('dige.png')
# #开运算:先腐蚀再膨胀--毛刺去掉
# kernel = np.ones((5,5),np.uint8)
# open = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
# #闭运算：先膨胀再腐蚀--还是原图像，毛刺并未去掉
# close = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
#
#
# res = np.hstack((img, open,close))
# cv2.imshow('erosion', res)
# cv2.imwrite('contrast3.png', res)
# #contrast3为原图、开、闭运算对比图
# cv2.waitKey(0)
# cv2.destroyAllWindows()
